package com.wwdx.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 读取配置文件
 * @author liguoliang
 * @date 2017-09-08
 */
public class ConfigReader {

    private Map<String, String> map = new LinkedHashMap<>();

    ConfigReader(String path) {
        Logger logger = LoggerFactory.getLogger(this.getClass());
        Properties p = new Properties();
        try (InputStream is = this.getClass().getResourceAsStream(path)){
            p.load(is);
        } catch (IOException e) {
            logger.error("错误信息：{}",e.getMessage());
        }
        //将Properties转换为Map
        CollectionUtils.mergePropertiesIntoMap(p, map);
    }

    public synchronized String get(String key) {
        return map.get(key);
    }
}